#include <GLES3/gl3.h>
#include <GLFW/glfw3.h>
#include <cmath>
#include <iostream>

#include <render/output/OGLRender.h>
#include <mesh/mesh.h>
#include <app/Atime.h>
#include <app/Application.h>

enum {
    POSITION_VB,
    NUM_BUFFERS
};

void OGLRender::mainCycle () {
    //if using GLFW
    if ( (*app).openGLTools.OpenGLContext == OGLC_GLFW ) {
        
        while ( !window -> isClosed () && (*app).running ) {
            glfwPollEvents ();
            glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

            (*app).update ();

            window -> Update(); 
        }
        
    //if using Qt tools
    } else if ( (*app).openGLTools.OpenGLContext == OGLC_GLFW ) {
        
        while ( !(*app).openGLTools.glWindowShouldClose && (*app).running ) {
            glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
            
            (*app).update ();
            
            (*app).openGLTools.glSwapBuffers ();
        }
    }
}

void OGLRender::joinToMainCycle () {
    mainCycle ();
}

OGLRender::OGLRender ( Application* appPtr, Window* win ) {
    app = appPtr;
    window = win;
}
